package com.example.dellpc.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.api.GetApi;
import com.example.model.User;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.w3c.dom.Text;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by 刘智强 on 2016/9/1.
 */
public class RetrofitActivity extends Activity {

    private String URL = "https://api.github.com";
    @ViewInject(R.id.tv_email)
    private TextView tv_email;
    @ViewInject(R.id.tv_creattime)
    private TextView tv_createtime;
    @ViewInject(R.id.btn_post)
    private Button btn_post;
    @ViewInject(R.id.pb)
    private ProgressBar pb;
    @ViewInject(R.id.rl_pb)
    private RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.retrofitactivity);
        ViewUtils.inject(this);

    }

    @OnClick(R.id.btn_post)
    public void onClick(Button btn) {

        switch (btn.getId()) {
            case R.id.btn_post:
                relativeLayout.setVisibility(View.VISIBLE);
                init();
                break;
            default:
        }
    }

    public void init() {
        RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint(URL).build();
        GetApi getApi = restAdapter.create(GetApi.class);
        getApi.getUserInfo("basil2style", new Callback<User>() {
            @Override
            public void success(User user, Response response) {
                Log.e("status", response.getStatus() + "");
                Log.e("body", response.getBody() + "");
                Log.e("reason", response.getReason() + "");
                Log.e("url", response.getUrl() + "");
                tv_email.setText(user.getEmail());
                tv_createtime.setText(user.getCreated_at());
                relativeLayout.setVisibility(View.GONE);
            }

            @Override
            public void failure(RetrofitError error) {
                Log.e("error", error.getMessage());
            }
        });

    }
}
